home *** CD-ROM | disk | FTP | other *** search
- /*
- File: OTFindSerialPorts.c
-
- Contains: Sample to show how to find all the serial ports using OT.
-
- Written by: Quinn "The Eskimo!"
-
- Copyright: © 1997 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- You may incorporate this sample code into your applications without
- restriction, though the sample code has been provided "AS IS" and the
- responsibility for its operation is 100% yours. However, what you are
- not permitted to do is to redistribute the source as "DSC Sample Code"
- after having made changes. If you're going to re-distribute the source,
- we require that you make it clear in the source that the code was
- descended from Apple Sample Code, but that you've made changes.
- */
-
- /////////////////////////////////////////////////////////////////////
- // The OT debugging macros in <OTDebug.h> require this variable to
- // be set.
-
- #ifndef qDebug
- #define qDebug 1
- #endif
-
- /////////////////////////////////////////////////////////////////////
- // Pick up all the standard OT stuff.
-
- #include <OpenTransport.h>
-
- /////////////////////////////////////////////////////////////////////
- // Pick up device type definitions.
-
- #include <OpenTptLinks.h>
-
- /////////////////////////////////////////////////////////////////////
- // Pick up special APIs for getting port information.
-
- #include <OpenTptConfig.h>
-
- /////////////////////////////////////////////////////////////////////
- // Pick up the OTDebugBreak and OTAssert macros.
-
- #include <OTDebug.h>
-
- /////////////////////////////////////////////////////////////////////
- // Standard C prototypes.
-
- #include <stdio.h>
-
- /////////////////////////////////////////////////////////////////////
- // OTDebugStr is not defined in any OT header files, but it is
- // exported by the libraries, so we define the prototype here.
-
- extern pascal void OTDebugStr(const char* str);
-
- /////////////////////////////////////////////////////////////////////
-
- static OSStatus PrintSerialPortInfo(const OTPortRecord *portRecord)
- // Prints information about the port with the given portRecord.
- {
- Str255 userVisibleName;
-
- // OTGetUserPortNameFromPortRef is a little known routine
- // from <OpenTptConfig.h> that allows you to get a user
- // visible name for an Open Transport port. You must
- // be running PPC code if you want to call this on a PPC machine.
-
- OTGetUserPortNameFromPortRef(portRecord->fRef, userVisibleName);
-
- printf("Found a serial port with port reference $%08lx:\n", portRecord->fRef);
- printf(" User visible name is “%#s”.\n", userVisibleName);
- printf(" String to pass to OTCreateConfiguration is “%s”.\n", portRecord->fPortName);
- printf(" Name of provider module is “%s”.\n", portRecord->fModuleName);
- printf("\n");
-
- return kOTNoError;
- }
-
- static OSStatus OTFindSerialPorts(void)
- // Lists all of the serial ports on the machine using Open Transport.
- {
- OSStatus err;
- Boolean portValid;
- SInt32 portIndex;
- OTPortRecord portRecord;
- UInt16 deviceType;
-
- // Start portIndex at 0 and call OTGetIndexedPort until
- // we find there are no more ports.
-
- portIndex = 0;
- err = kOTNoError;
- do {
- portValid = OTGetIndexedPort(&portRecord, portIndex);
- if (portValid) {
-
- // For each valid port, get the deviceType and, if
- // it's a serial port and not an alias, call PrintSerialPort
- // to dump out its information. Note that you don't want
- // to include aliases to the serial ports in the list, otherwise
- // a standard machine will have 3 serial ports, "serialA", "serialB"
- // and "serial".
-
- deviceType = OTGetDeviceTypeFromPortRef(portRecord.fRef);
- if (deviceType == kOTSerialDevice &&
- (portRecord.fInfoFlags & kOTPortIsAlias) == 0) {
- err = PrintSerialPortInfo(&portRecord);
- }
- }
- portIndex += 1;
- } while ( portValid && err == kOTNoError);
-
- return err;
- }
-
- /////////////////////////////////////////////////////////////////////
-
- void main(void)
- {
- OSStatus err;
-
- printf("OTFindSerialPorts\n");
- printf("-- Lists all the serial ports on the machine using OT\n");
- printf("\n");
-
- err = InitOpenTransport();
-
- if (err == noErr) {
-
- err = OTFindSerialPorts();
-
- CloseOpenTransport();
- }
-
- if (err == noErr) {
- printf("Success.\n");
- } else {
- printf("Failed with error %d.\n", err);
- }
- printf("Done. Press command-Q to Quit.\n");
- }
-